package com.example.shop.commodity.controller;

import com.example.shop.base.BaseController;
import com.example.shop.base.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import com.example.shop.commodity.entity.ShopCart;
import com.example.shop.commodity.service.ShopCartService;


/**
 * @author   hcj
 * @date 2021-09-21 19:06:31
*/
@Controller
@RequestMapping(value = "/shop/cart")
public class ShopCartController extends BaseController{

    @Autowired
    private ShopCartService shopCartService;

    @ModelAttribute
    public ShopCart get(@RequestParam(required=false) String id) {
        if (StringUtils.isNotBlank(id)){
            ShopCart shopCart = new ShopCart();
            shopCart.setId(id);
            return shopCartService.get(shopCart);
        }else{
            return new ShopCart();
        }
    }

    @RequestMapping(value = "/get")
    @ResponseBody
    public ShopCart getById(ShopCart shopCart) {
        return shopCart;
    }

    @RequestMapping(value = "/list")
    public String list(ShopCart shopCart) {
        return "shopCart";
    }

    @RequestMapping(value = "/info")
    @ResponseBody
    public List<ShopCart> info(ShopCart shopCart) {
        return shopCartService.findList(shopCart);
    }


    @RequestMapping(value = "/findPage")
    @ResponseBody
    public PageInfo<ShopCart> findPage(ShopCart shopCart) {
        return  shopCartService.findPage( shopCart);
    }

    @RequestMapping(value = "/save")
    @ResponseBody
    public Result save(ShopCart shopCart) {
        shopCartService.save(shopCart);
        return Result.success("保存成功");
    }

    @RequestMapping(value = "/logicalDeletion",method = RequestMethod.POST)
    @ResponseBody
    public Result logicalDeletion(@RequestParam("id") String id){
        shopCartService.logicalDeletion(id);
        return Result.success();
    }

    @RequestMapping(value = "/delete",method = RequestMethod.POST)
    @ResponseBody
    public Result delete(@RequestParam("ids") String[] ids){
        shopCartService.delete(ids);
        return Result.success();
    }

}